package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.mapper.YddArticleMapper;
import com.itheima.ydd.service.IYddArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.service.IYddCateService;
import com.itheima.ydd.vo.HotVo;
import com.itheima.ydd.vo.MyArticleVo;
import com.itheima.ydd.vo.YddArticleCateVo;
import com.itheima.ydd.vo.YddArticleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements IYddArticleService {

    @Autowired
    private IYddCateService yddCateService;

    @Autowired
    private IYddArticleService yddArticleService;

    @Override
    public Page<YddArticleVo> pageByCondition(Page<YddArticle> pageInfo, LambdaQueryWrapper<YddArticle> queryWrapper) {
        //查询
        this.page(pageInfo, queryWrapper);
        //拷贝数据
        Page<YddArticleVo> yddArticleVoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, yddArticleVoPage, "records");
        List<YddArticle> records = pageInfo.getRecords();
        List<YddArticleVo> yddArticleVos = records.stream().map((yddArticle) -> {
            YddArticleVo yddArticleVo = new YddArticleVo();
            BeanUtils.copyProperties(yddArticle, yddArticleVo);
            YddCate byId = yddCateService.getById(yddArticle.getCateid());
            yddArticleVo.setCatename(byId.getCatename());
            if(yddArticle.getCreateDate()!=null){yddArticleVo.setCreate_date(yddArticle.getCreateDate());}
            if (yddArticle.getUpdateDate()!=null){yddArticleVo.setUpdate_date(yddArticle.getUpdateDate());}
            return yddArticleVo;
        }).collect(Collectors.toList());
        //返回数据
        yddArticleVoPage.setRecords(yddArticleVos);
        return yddArticleVoPage;
    }
    /**
     * 热门置顶文章数据(首页)
     * 接口  new
     *
     * @param pageInfo //     * @param newVoQueryWrapper
     * @param wrapper
     * @return
     */
    @Override
    public HashMap<String, Object> selectByPage(Page<YddArticle> pageInfo, LambdaQueryWrapper<YddArticle> wrapper) {
        //根据分页模型以及构造条件,进行条件分页查询文章数据
        Page<YddArticle> yddArticlePage = this.page(pageInfo,wrapper);

        //获取dish中records集合数据
        List<YddArticle> records = yddArticlePage.getRecords();

        //对records集合  进行操作
        List<Object> articleCateList = records.stream().map(aVoid -> {
            YddArticleCateVo vo = new YddArticleCateVo();
            //将records中的数据 复制到 vo 中
            BeanUtils.copyProperties(aVoid, vo);
            //根据Cateid来查询gCateName
            YddCate yddCate = yddCateService.getById(aVoid.getCateid());
            String categoryName = yddCate.getCatename();
            vo.setCatename(categoryName);
            return vo;
        }).collect(Collectors.toList());//将流中的数据整理为List

        //24小时最新文章数量
       /* //获取当前时间
        long toDay = System.currentTimeMillis();
        //24小时的时间
        Long beForeDay =  toDay - toDay * 60 * 60 * 24;*/
        LambdaQueryWrapper<YddArticle> queryWrapperDay = new LambdaQueryWrapper<>();
        queryWrapperDay.between(YddArticle::getCreateDate,"1572349100000","1668782345942");
        int countDay = yddArticleService.count(queryWrapperDay);

        //一周文章最新数量
        //一周以内的时间
//        Long beForeWeek = toDay - toDay * 60 * 60 * 24 * 7;
        LambdaQueryWrapper<YddArticle> queryWrapperWeek = new LambdaQueryWrapper<>();
        queryWrapperWeek.between(YddArticle::getCreateDate,"1572349100000","1668782345942");
        int countWeek = yddArticleService.count(queryWrapperWeek);


        //将数据放入maos集合
        Map<String, Object> maps = new HashMap<>();
        maps.put("count", yddArticlePage.getTotal()); //总条数
        maps.put("totalPages", yddArticlePage.getPages());  //总页数
        maps.put("pageSize", yddArticlePage.getSize()); //显示几条数据
        maps.put("currentPage", yddArticlePage.getCurrent()); //显示的页数
        maps.put("data", articleCateList);

        HashMap<String, Object> map = new HashMap<>();
        map.put("desc","首页大列表");
        map.put("num_day",countDay);
        map.put("num_week",countWeek);
        map.put("list",maps);

        return map;
    }
    /**
     * 我的文章
     *
     * @param pageInfo
     * @param queryWrapper
     * @return
     */
    @Override
    public MyArticleVo selectByMyArticlePage(Page<YddArticle> pageInfo, LambdaQueryWrapper<YddArticle> queryWrapper) {
        //根据分页模型以及构造条件,进行条件分页查询文章数据
        Page<YddArticle> yddArticlePage = this.page(pageInfo, queryWrapper);
        //获取 yddArticlePage 中 records 集合数据
        List<YddArticle> records = yddArticlePage.getRecords();

        //创建收集数据收据对象
        MyArticleVo myArticleVo = new MyArticleVo();
        //封装数据
        myArticleVo.setData(records);
        myArticleVo.setCount(yddArticlePage.getTotal());
        myArticleVo.setTotalPages(yddArticlePage.getPages());
        myArticleVo.setPageSize(yddArticlePage.getSize());
        myArticleVo.setCurrentPage(yddArticlePage.getCurrent());

        return myArticleVo;
    }

    /**
     * 最新文章
     *
     * @return
     */
    @Override
    public HotVo YddArticleCatelist() {

        //查询条件
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(YddArticle::getCreateDate,"1572349100000","1668782345942")
                .select(YddArticle::getId, YddArticle::getTitle, YddArticle::getPic, YddArticle::getLovenum, YddArticle::getCateid);
        //获取ydd_articles表格数据
        List<YddArticle> yddArticles = yddArticleService.list(queryWrapper);

        //遍历数据  获取 ydd_cate的cateName
        List<YddArticleCateVo> articleCateVos = yddArticles.stream().map(item -> {
            //接收查到的收据
            YddArticleCateVo articleCateVo = new YddArticleCateVo();

            //将查到的数据 复制到 articleCateVo 中
            BeanUtils.copyProperties(item, articleCateVo);
            // TODO:将查到的数据 复制到 articleCateVo 中 忽略Id不复制
//            BeanUtils.copyProperties(item, articleCateVo,new String("id"));

            Integer cateid = item.getCateid();

            YddCate yddCate = yddCateService.getById(cateid);
            articleCateVo.setCatename(yddCate.getCatename());

            return articleCateVo;
        }).collect(Collectors.toList());//将流中的数据整理为List

        //创建对象  留有返回用
        HotVo hotVo = new HotVo();
        hotVo.setDesc("侧边最新文章");
        hotVo.setList(articleCateVos);

        return hotVo;
    }
}
